<?php
class basedao 
{
    public function dao2class($dao)
    {
      $array = explode('dao', $dao); 
      return $array[0];
    }


    public function fing_by_condition($condition)
    {
    
    }
    public function loadrelation($entity)
    {
        $relations = $entity->getrelation();
        foreach($relations as $relation)
        {
            $relation->addpendref($entity);
        }
    
    }

    public function find($ids)
    {
        $dao = get_class($this);
        $class = $this->dao2class($dao);
        $sql = "select * from $class where id in (:ids)";
        $results = matrix::get('dbexecuter')->query($sql, array(':ids' => $ids));
        if(is_numeric($ids))
        {
            $instance = null;
            foreach($results as $entityinfos)
            {
                $instance = new $class(array('entity_onload'=>true));
                foreach($entityinfos as $attribute => $entityinfo)
                {
                    $instance->$attribute = $entityinfo;    
                }
                entityrepo::put($instance);
                $this->loadrelation($instance);
                $instance->setallfalse();
            }
            return $instance;
        }

        if(is_array($ids))
        {
            $instancelist = array();
            foreach($results as $entityinfos)
            {
                $instance = new $class(array('entity_onload'=>true));
                foreach($entityinfos as $attribute => $entityinfo)
                {
                    $instance->$attribute = $entityinfo;    
                }
                $instancelist[$instance->id] = $instance;
                entityrepo::put($instance);
                $this->loadrelation($instance);
                // ->_ref 时候 判断 如果 _ref是 relation的时候 果断走 relation 的 loadbymethod;
                $instance->setallfalse();
            }
            return $instancelist;
        }
    }
}
